﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PIM_ATE
{
    public partial class FrmManualPIM : Form
    {
        public FrmManualPIM(ClsDataUser user)
        {
            InitializeComponent();
            m_userObj = user;
            m_pim_configObj = new ClsDataConfigLocalBuilder(GDefine.DBNamePIM);
            m_pim_resultObj = new ClsDataResultForAntennaBuilder(GDefine.DBNamePIM);
            m_pim_dicResults = new Dictionary<int, JcPIMDataMaxResult>();
            m_selectBuilder = new ClsDataConfigSelectBuilder(GDefine.DBNamePIM);
        }

        Label[] ctl_config_list_lblName;
        TextBox[] ctl_config_list_tBItemValue;

        //获取配置实例
        ClsDataConfigLocalBuilder m_pim_configObj;
        //上传数据实例
        ClsDataResultForAntennaBuilder m_pim_resultObj;
        //配置列表
        List<ClsDataConfigLocal> m_pim_lstCfgData_local;
        //用户
        ClsDataUser m_userObj;
        Dictionary<int, JcPIMDataMaxResult> m_pim_dicResults;
        DataTable m_pim_dtShow;
        ClsDataProduct productObj;
        ClsDataConfigSelectBuilder m_selectBuilder;

        private void FrmManual_Load(object sender, EventArgs e)
        {
            cbPIMStep.SelectedIndex = 0;
        }

        private void ctl_main_btnSet_Click(object sender, EventArgs e)
        {
            if (tbProductSN.Text == "") return;
            if (initDataConfig_for_antenna(tbProductSN.Text))
            {
                ptlControls.Controls.Clear();
                createCtl();
            }
            
        }

        private bool initDataConfig_for_antenna(string sProductSN)
        {
            if (m_pim_configObj == null)
            {//PIM模式
                m_pim_configObj = new ClsDataConfigLocalBuilder("DEFAULT");
            }

            productObj = new ClsDataProduct();
            //产品追溯号
            productObj.sProductSN = sProductSN;

            if (!matchProcode(productObj))
            {//匹配条码，下载配置可选信息
                MessageBox.Show("无法匹配条码：" + productObj.sProductSN);
                return false;
            }

            m_pim_configObj.constructByProcode(productObj.sProcode);
            if (m_pim_configObj.construct(productObj.sProcode, "1.0"))
            {//根据选择下载配置
                m_pim_lstCfgData_local = m_pim_configObj.download(out m_pim_dtShow);
            }
            else
            {
                MessageBox.Show("未发现配置：" + productObj.sProcode);
                return false;
            }

            if (!m_pim_resultObj.constructSN(tbProductSN.Text, cbPIMStep.SelectedItem.ToString(), m_userObj))
            {
                return false;
            }

            if (productObj.enableConfig.Count == 0)
            {//无配置时
                for (int i = 0; i < m_pim_lstCfgData_local.Count; i++)
                    productObj.enableConfig.Add(true);
            }
            else
            {
                if (m_pim_lstCfgData_local.Count != productObj.enableConfig.Count)
                {//配置数量错误
                    MessageBox.Show("产品代码的可选配置错误！");
                    return false;
                }
            }

            ctl_main_tbProCode.Text = productObj.sProcode;
            //createCtl();
            return true;
        }

        private bool matchProcode(ClsDataProduct product)
        {
            if (!m_pim_resultObj.matchSN(product))
            {//匹配条码
                return false;
            }

            m_selectBuilder.construct(product.sMOID);
            List<bool> lstEnable = m_selectBuilder.downloadPIMSelect();
            if (lstEnable == null)
            {//查询配置可选
                return false;
            }
            product.enableConfig = lstEnable;

            return true;
        }

        private void createCtl()
        {
            ptlControls.RowCount = m_pim_lstCfgData_local.Count / 2 + 1;
            ctl_config_list_lblName = new Label[m_pim_lstCfgData_local.Count];
            ctl_config_list_tBItemValue = new TextBox[m_pim_lstCfgData_local.Count];

            for (int i = 0; i < m_pim_lstCfgData_local.Count; i++)
            {
                if (!productObj.enableConfig[i]) continue;

                ctl_config_list_lblName[i] = new Label();
                ctl_config_list_lblName[i].Anchor = AnchorStyles.None;
                ctl_config_list_lblName[i].AutoSize = true;
                ctl_config_list_lblName[i].Size = new System.Drawing.Size(47, 12);
                ctl_config_list_lblName[i].Text = m_pim_lstCfgData_local[i].BandAlias + "/" +
                                                  m_pim_lstCfgData_local[i].RCUIndex + "/" +
                                                  m_pim_lstCfgData_local[i].TestAngle+"/"+
                                                  m_pim_lstCfgData_local[i].SpecReg;

                //ctl_config_list_lblName[i].Text = m_pim_lstCfgData_local[i].PIMItemName;

                ptlControls.Controls.Add(ctl_config_list_lblName[i]);

                ctl_config_list_tBItemValue[i] = new TextBox();
                ctl_config_list_tBItemValue[i].Anchor = AnchorStyles.Left | AnchorStyles.Right;

                ptlControls.Controls.Add(ctl_config_list_tBItemValue[i]);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool isAllNull = true;

            if (productObj == null || productObj.sProductSN == "")
            {
                MessageBox.Show("请输入条码和信息后设定!");
                return;
            }

            if (tbProductSN.Text == "" || cbPIMStep.SelectedIndex < 0)
            {
                MessageBox.Show("请输入条码和信息!");
                return;
            }

            try
            {                
                for (int i = 0; i < m_pim_lstCfgData_local.Count; i++)
                {
                    if (!productObj.enableConfig[i]) continue;
                    if (ctl_config_list_tBItemValue[i].Text.Replace(" ", "") == "")
                    {
                        MessageBox.Show("请完整输入，不测试的项目请输入0");
                        return;
                    }

                    float max = Convert.ToSingle(ctl_config_list_tBItemValue[i].Text);
                    if (max == 0) continue;
                    if (max > 0) max = max * -1;
                    bool bResult = max > Convert.ToSingle(m_pim_lstCfgData_local[i].SpecReg);

                    JcPIMDataMaxResult result = new JcPIMDataMaxResult();
                    result.fValue = max;
                    result.sPass = bResult ? GDefine.ContentFail : GDefine.ContentPass;
                    m_pim_dicResults.Add(i, result);
                    isAllNull = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("测试值的数据格式不正确，请检查!");
                return;
            }

            if (isAllNull)
            {
                MessageBox.Show("没有测试值，请输入!");
                return;
            }

            if (m_pim_resultObj.upload(m_pim_dicResults, m_pim_lstCfgData_local))
            {
                //MessageBox.Show("上传成功！");
                string res_pass = GetResultPass(m_pim_dicResults, m_pim_lstCfgData_local);
                MessageBox.Show("上传成功！测试结果:" + res_pass);
                btnClear_Click(null, null);
                return;
            }

        }
        private string GetResultPass(Dictionary<int, JcPIMDataMaxResult> dicResults, List<ClsDataConfigLocal> lstConfig)
        {
            float fAllMax = float.MinValue;
            //string sAllPass = "";
            string sAllPass = "";

            for (int i = 0; i < lstConfig.Count; i++)
            {//生成测试项数据

                #region Get Result

                string sMax, sPass;
                if (dicResults.ContainsKey(i))
                {
                    sPass = dicResults[i].sPass;
                    sMax = dicResults[i].fValue.ToString();
                    if (fAllMax < dicResults[i].fValue)
                    {
                        fAllMax = dicResults[i].fValue;
                        sAllPass = sPass;
                    }
                }
                else
                {
                    continue;
                }
                #endregion
            }
            return sAllPass;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            tbProductSN.Text = "";
            ctl_main_tbProCode.Text = "";
            m_pim_dicResults.Clear();
            ptlControls.Controls.Clear();
            productObj = null;
            //foreach (var item in ctl_config_list_tBItemValue)
            //{
            //    item.Text = "";
            //}
        }

        private void tbProductSN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                ctl_main_btnSet_Click(sender, e);
            }
        }

    }
}
